#coding=utf-8
#Boa:Dialog:DlgProductStock

import wx
import wx.grid

from product import *

def create(parent):
    return DlgProductStock(parent)

[wxID_DLGPRODUCTSTOCK, wxID_DLGPRODUCTSTOCKBTNCANCEL, 
 wxID_DLGPRODUCTSTOCKBTNSAVE, wxID_DLGPRODUCTSTOCKGRID1, 
] = [wx.NewId() for _init_ctrls in range(4)]

class DlgProductStock(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPRODUCTSTOCK,
              name=u'DlgProductStock', parent=prnt, pos=wx.Point(797, 360),
              size=wx.Size(847, 439), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5546\u54c1\u5e93\u5b58')
        self.SetClientSize(wx.Size(831, 401))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.btnSave = wx.Button(id=wxID_DLGPRODUCTSTOCKBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(240, 320), size=wx.Size(93, 26), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGPRODUCTSTOCKBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGPRODUCTSTOCKBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(464, 320), size=wx.Size(93, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGPRODUCTSTOCKBTNCANCEL)

        self.grid1 = wx.grid.Grid(id=wxID_DLGPRODUCTSTOCKGRID1, name='grid1',
              parent=self, pos=wx.Point(32, 80), size=wx.Size(752, 184),
              style=0)
        self.grid1.SetDefaultColSize(70)
        self.grid1.SetDefaultRowSize(25)
        self.grid1.SetRowLabelSize(15)
        self.grid1.SetColLabelSize(25)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        #单位表格
        titles = [(u'仓库ID', 50),
                    (u'代码', 50),
                    (u'仓库', 100), 
                    (u'库存', 100), 
                    (u'仓位1', 50), 
                    (u'备注1', 100),
                    (u'仓位2', 50), 
                    (u'备注2', 100),
                    (u'库存备注', 300)]
        [self.COL_ID, self.COL_CODE, self.COL_NAME, self.COL_QTY
        , self.COL_POS1, self.COL_MEMO1, self.COL_POS2, self.COL_MEMO2
        ,  self.COL_MEMO] = [i for i in range(len(titles))]
                        
        self.grid1.CreateGrid(3, len(titles))
        for i in range(len(titles)):
            self.grid1.SetColLabelValue(i, titles[i][0])
            self.grid1.SetColSize(i, titles[i][1])
        #self.grid1.SetSize(self.grid1.GetBestSize())
        
        attr = wx.grid.GridCellAttr()
        attr.SetReadOnly(True)
        attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
        self.grid1.SetColAttr(self.COL_ID, attr)
        self.grid1.SetColAttr(self.COL_CODE, attr)
        self.grid1.SetColAttr(self.COL_NAME, attr)
        
        attr = wx.grid.GridCellAttr()
        attr.SetEditor(wx.grid.GridCellFloatEditor(precision=2))
        attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE)
        self.grid1.SetColAttr(self.COL_QTY, attr)
        
    def SetProduct(self, proId):
        self.m_proId = proId
        list = CDataProduct.GetStockInfo(proId)
        
        for row,item in enumerate(list):
            #wh_id,wh_name,wh_pos,qty,memo = item
            for col,val in enumerate(item):
                self.grid1.SetCellValue(row, col, kit.myStr(val) if val is not None else "")

    def GetGridData(self, grid):
        grid_data = []  
        for i in range(grid.GetNumberRows()):  
            row_data = []  
            for j in range(grid.GetNumberCols()):  
                value = grid.GetCellValue(i, j)  
                row_data.append(value)
            grid_data.append(row_data)
        return grid_data
        
    def OnBtnSaveButton(self, event):
        CDataProduct.UpdateStockInfo(self.m_proId, self.GetGridData(self.grid1))
        # 没什么意义，只能更新本地的看一下是不是设置成功了，对其他分机还是不生效的
        proObj = CDataProduct.getData({'id':self.m_proId})
        proObj.refreshOnhand()
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
